<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\User;
use think\exception\ValidateException;
use think\Response;
use WeMini\Crypt;

class Login extends Base
{

    /**
     * 原点 2022年2月16日14:16:31 小程序授权登录
     */
    public function miniAppletLogin(): Response
    {
        try {

            $param = $this->request->post();
            $this->validate($param, 'MiniAppletLogin');
            $result = (new Crypt(config('pay')['wechat']))->session($param['code']);

            $result['nickname'] = $param['nickname'];
            $result['photo'] = $param['photo'];
            return json(User::_miniAppletSave($result));

        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

}
